/**=========================================================
 * Module: sparkline.js
 * SparkLines Mini Charts
 =========================================================*/
 
App.directive('sparkline', ['$timeout', '$window', 'Utils', 
    function($timeout, $window, utils){

        'use strict';

        return {
            restrict: 'EA',
            scope: {
                watchVar: '='
            },
            link: function($scope, $element, $attrs){
                var runSL = function() {
                    initSparLine($element);
                };

                $timeout(runSL);

                $scope.$watch("watchVar", 
                    function (newVal, oldVal) {
                        if (!utils.isEmpty(newVal)){
                            runSL();
                        }
                    }
                );

                if (!utils.isEmpty($attrs.name)){
                    $scope.$parent[$attrs.name] = $element;
                } 
            }
        };

        function initSparLine($element) {
            var options = $element.data();

            options.type = options.type || 'line'; // default chart is bar
            options.disableHiddenCheck = true;

            $element.sparkline('html', options);

            if(options.resize) {
                $(window).resize(function(){
                    $element.sparkline('html', options);
                });
            }
        }

    }
]);
